package com.platform.modules.alg.alglib.hdu1232;

public class Hdu1232 {
    public String output = "";

    private int N = 1010;
    int fa[] = new int[N];

    int Find(int x) {
        if (x != fa[x])
            fa[x] = Find(fa[x]);
        return fa[x];
    }

    void Union(int x, int y) {
        int a = Find(x);
        int b = Find(y);
        if (a != b)
            fa[a] = b;
    }

    public String cal(String input) {
        int n, m, i, ans = 0, x, y;
        String[] line = input.split("\n");
        String[] words = line[0].split(" ");
        n = Integer.parseInt(words[0]);
        m = Integer.parseInt(words[1]);

        for (i = 1; i <= n; i++)
            fa[i] = i;
        for (i = 0; i < m; i++) {
            String[] connect = line[i + 1].split(" ");
            x = Integer.parseInt(connect[0]);
            y = Integer.parseInt(connect[1]);
            Union(x, y);
        }
        for (i = 1; i <= n; i++) {
            if (i == fa[i])
                ans++;
        }

        return output = "" + (ans - 1);
    }
}
